//
//  LoginViewController.m
//  KuWeiPark
//
//  Created by Kevin Su on 15/4/28.
//  Copyright (c) 2015年 Clief. All rights reserved.
//

#import "LoginViewController.h"
#import "HomeViewController.h"
#import "SVProgressHUD.h"
#import "HttpClientManager.h"
#import "DataModel.h"
#import "AppMacro.h"
#import "AppDelegate.h"

@interface LoginViewController ()<UITextFieldDelegate>

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.loginBtn.layer.masksToBounds = YES;
    self.loginBtn.layer.cornerRadius = 5;
    
    self.phoneTextField.delegate = self;
    self.vercodeTextField.delegate = self;
    
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard)]];
}

- (void)viewWillAppear:(BOOL)animated{
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated{
    
    [self hideKeyBoard];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - IBAction
- (IBAction)sendVercodeAction:(id)sender {
    
    //获取验证码
}

- (IBAction)loginAction:(id)sender {
    
    if(self.phoneTextField.text == nil || self.phoneTextField.text.length == 0){
        
        [SVProgressHUD showErrorWithStatus:@"请输入手机号"];
        return;
    }
    
    if(self.vercodeTextField.text == nil || self.vercodeTextField.text.length == 0){
        
        [SVProgressHUD showErrorWithStatus:@"请输入验证码"];
        return;
    }
    
    //登录
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
    [[HttpClientManager sharedClient] loginRequestWithMobilePhone:self.phoneTextField.text WithAuthCode:self.vercodeTextField.text WithSucessBlock:^(JsonRetMsg *response) {
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD showSuccessWithStatus:@"登录成功"];
        });
        LoginModel *loginModel = [[LoginModel alloc] initWithDictionary:response.data error:nil];
        ApplicationDelegate.loginModel = loginModel;
        NSMutableDictionary *resultDic = [[NSMutableDictionary alloc] initWithDictionary:response.data];
        for(NSString *key in ((NSDictionary *)response.data).allKeys){
            
            resultDic[key] = VALID_STRING(response.data[key]);
        }
        [[NSUserDefaults standardUserDefaults] setObject:resultDic forKey:NsUserDefaultKey_LoginModel];
        
        HomeViewController *homeViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"HomeViewController"];
        [self.navigationController pushViewController:homeViewController animated:YES];
        
    } withFailedBlock:^(JsonRetMsg *responseObject) {
        
        [SVProgressHUD showErrorWithStatus:@"登录失败"];
    }];
}

- (IBAction)qqLoginAction:(id)sender {
    
    //QQ登录
}

- (IBAction)weichatLoginAction:(id)sender {
    
    //微信登录
}

- (IBAction)sinaWeiboLoginAction:(id)sender {
    
    //新浪微博登录
}

- (IBAction)back:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - TextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self hideKeyBoard];
    return YES;
}

- (void)hideKeyBoard{
    
    [self.phoneTextField resignFirstResponder];
    [self.vercodeTextField resignFirstResponder];
}

@end
